using System;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.UI;
using System.Collections.Generic;
using System.Data;
using System.Drawing;

namespace Googler.BLL
{
    /// <summary>
    /// Summary description for CommonFunctions.
    /// </summary>
    public class CommonFunctions
    {
        static CommonFunctions()
        {
            CommonFunctions.chControls = new ArrayList();
        }

        public static void CommandButtonEventHandlerConfirm(DataGrid dg, int columnNumber)
        {
            foreach (DataGridItem item1 in dg.Items)
            {
                foreach (Control control1 in item1.Cells[columnNumber].Controls)
                {
                    if (control1 is Button)
                    {
                        ((Button)control1).Attributes["OnClick"] = " javascript:return confirm('Are you sure you want to handle the alert? ');";
                    }
                }
            }
        }
        /// <summary>
        /// Add a deleting confirm to certain column in the datagrid this overload assumes that the target coulmn index is "1"
        /// </summary>
        /// <param name="dg">The datagrid which contains the target column</param>
        /// <param name="columnIndex">The target column Index</param>
        public static void AddDeletingConfirm(System.Web.UI.WebControls.DataGrid dg, int columnIndex)
        {
            foreach (System.Web.UI.WebControls.DataGridItem item in dg.Items)
            {
                foreach (Control cntl in item.Cells[columnIndex].Controls)
                    if (cntl is ImageButton)
                        ((ImageButton)cntl).Attributes["OnClick"] = " javascript:return confirm('Are you sure you want to delete ? ');";
                    else if (cntl is LinkButton)
                        ((LinkButton)cntl).Attributes["OnClick"] = " javascript:return confirm('Are you sure you want to delete ? ');";
            }
        }
        /// <summary>
        /// Add a deleting confirm to certain column in the datagrid this overload assumes that the target coulmn index is "1"
        /// </summary>
        /// <param name="dg">The datagrid which contains the target column</param>
        public static void AddDeletingConfirm(System.Web.UI.WebControls.DataGrid dg)
        {
            AddDeletingConfirm(dg, 1);

        }
        public static void AddDeletingConfirm(Button btn)
        {
            btn.Attributes["OnClick"] = " javascript:return confirm('Are you sure you want to delete ? ');";
        }
        public static void AddDeletingConfirm(LinkButton btn)
        {
            btn.Attributes["OnClick"] = " javascript:return confirm('Are you sure you want to delete ? ');";
        }


        public static int FillListItems<T>(ListItemCollection listitems, List<T> items)
        {
            listitems.Clear();
            foreach (IListItem item1 in items)
            {
                ListItem item2 = new ListItem(item1.Text.ToString(), item1.OId.ToString());
                listitems.Add(item2);
            }
            return listitems.Count;
        }
        public static int FillListItems(ListItemCollection listitems, CollectionBase items)
        {
            listitems.Clear();
            foreach (IListItem item1 in items)
            {
                ListItem item2 = new ListItem(item1.Text.ToString(), item1.OId.ToString());
                listitems.Add(item2);
            }
            return listitems.Count;
        }
        public static int FillListItems(ListItemCollection listitems, IList items)
        {
            listitems.Clear();
            foreach (IListItem item1 in items)
            {
                ListItem item2 = new ListItem(item1.Text.ToString(), item1.OId.ToString());
                listitems.Add(item2);
            }
            return listitems.Count;
        }
        public static int FillListItems(ListItemCollection listitems, DataTable items)
        {
            listitems.Clear();
            foreach (DataRow row1 in items.Rows)
            {
                ListItem item1 = new ListItem(row1["Text"].ToString(), row1["OId"].ToString());
                listitems.Add(item1);
            }
            return listitems.Count;
        }


        public static int FillListItemsAndAddOther(ListItemCollection listitems, CollectionBase items)
        {
            listitems.Clear();
            foreach (IListItem itm in items)
            {
                ListItem item = new ListItem(itm.Text.ToString(), itm.OId.ToString());
                listitems.Add(item);
            }
            ListItem otheritem = new ListItem("---Other---", "");
            listitems.Add(otheritem);
            return listitems.Count;

        }
        public static int FillListItemsAndAddEmpty(ListItemCollection listitems, CollectionBase items)
        {
            listitems.Clear();
            foreach (IListItem itm in items)
            {
                ListItem item = new ListItem(itm.Text.ToString(), itm.OId.ToString());
                listitems.Add(item);
            }
            ListItem otheritem = new ListItem("", "0");
            listitems.Insert(0, otheritem);
            return listitems.Count;

        }
        public static int FillListItemsWithZeroValueField(ListItemCollection listitems, IList items)
        {
            listitems.Clear();
            ListItem item = new ListItem("Select", "0");
            listitems.Add(item);
            foreach (IListItem itm in items)
            {
                item = new ListItem(itm.Text.ToString(), itm.OId.ToString());
                listitems.Add(item);
            }
            return listitems.Count;

        }
        public static int FillListItemsWithNoSelection(ListItemCollection listitems, CollectionBase items)
        {
            listitems.Clear();
            ListItem item1 = new ListItem(string.Empty, "0");
            listitems.Add(item1);
            foreach (IListItem item2 in items)
            {
                item1 = new ListItem(item2.Text.ToString(), item2.OId.ToString());
                listitems.Add(item1);
            }
            return listitems.Count;
        }
        public static int FillListItemsWithSelect(ListItemCollection listitems, CollectionBase items)
        {
            listitems.Clear();
            ListItem item1 = new ListItem("Select", "0");
            listitems.Add(item1);
            foreach (IListItem item2 in items)
            {
                item1 = new ListItem(item2.Text.ToString(), item2.OId.ToString());
                listitems.Add(item1);
            }
            return listitems.Count;
        }
        public static int FillWithDataTable(ListItemCollection listitems, DataTable dt, bool AddSelect)
        {
            listitems.Clear();
            if (AddSelect)
            {
                ListItem item1 = new ListItem("Select", "0");
                listitems.Add(item1);
            }
            foreach (DataRow row1 in dt.Rows)
            {
                ListItem item2 = new ListItem(row1["Text"].ToString(), row1["OId"].ToString());
                listitems.Add(item2);
            }
            return listitems.Count;
        }

        static ArrayList chControls = new ArrayList();
        private static ArrayList GetControls(Control container)
        {
            if (container.HasControls())
            {
                foreach (Control cntrl in container.Controls)
                    GetControls(cntrl);
            }
            else
                chControls.Add(container);
            #region Commented code
            //			foreach(System.Web.UI.Control cntrl in  tbl.Controls)
            //			{
            //				if(cntrl.GetType().Name=="HtmlTableRow")
            //				{
            //					foreach(System.Web.UI.Control rowcntrl in ((HtmlTableRow)cntrl).Controls)
            //					{
            //						if(rowcntrl.GetType().Name!="Label")
            //							if(rowcntrl.GetType().Name!="RequiredFieldValidator")
            //								if(rowcntrl.GetType().Name!="RegularExpressionValidator")
            //									tblControls.Add(rowcntrl);
            //					}
            //				}
            //				if(cntrl.GetType().Name!="Label")
            //					if(cntrl.GetType().Name!="RequiredFieldValidator")
            //						if(cntrl.GetType().Name!="RegularExpressionValidator")
            //							tblControls.Add(cntrl);
            //
            //				
            //			}
            //			return tblControls;
            #endregion
            return chControls;
        }
        public static void DisableChControls(Control container)
        {

            GetControls(container);
            if (chControls.Count > 0)
            {
                foreach (object cntrl in chControls)
                {
                    if (cntrl is TextBox)
                        ((TextBox)cntrl).Enabled = false;
                    if (cntrl is DropDownList)
                        ((DropDownList)cntrl).Enabled = false;
                    if (cntrl is RadioButtonList)
                        ((RadioButtonList)cntrl).Enabled = false;
                }
            }
        }
        public static void EnableChControls(Control container)
        {

            GetControls(container);
            if (chControls.Count > 0)
            {
                foreach (object cntrl in chControls)
                {
                    if (cntrl is TextBox)
                        ((TextBox)cntrl).Enabled = true;
                    if (cntrl is DropDownList)
                        ((DropDownList)cntrl).Enabled = true;
                    if (cntrl is RadioButtonList)
                        ((RadioButtonList)cntrl).Enabled = true;
                }
            }
        }


        public static void SelectValue(DropDownList ddl, string value)
        {
            ListItem item1 = ddl.Items.FindByValue(value);
            if (item1 != null)
            {
                ddl.SelectedValue = item1.Value;
            }
            else if (ddl.Items.Count > 0)
            {
                ddl.SelectedIndex = 0;
            }
        }

        public static void WriteWithColor(Label textBoard, string text, Color textColor)
        {
            textBoard.Text = string.Format("<font color='{0}'>{1}</font>", ColorTranslator.ToHtml(textColor), text);
        }

        public static string AdjustEnglishName(string enName)
        {
            enName = enName.Trim();
            if (enName != "")
            {
                string text1 = enName.Substring(0, 1).ToUpper();
                enName = text1 + enName.Remove(0, 1);
            }
            return enName;
        }

        public static string DeleteLastDot(string NumericText)
        {
            if ((NumericText != "") && NumericText.EndsWith("."))
            {
                NumericText = NumericText.Remove(NumericText.Length - 1);
            }
            return NumericText;
        }
    }
}